package com.mzp.userFavorGoods.userFavorGoods.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.mzp.userFavorGoods.userFavorGoods.dao.userFavorGoodsDao;
import com.mzp.userFavorGoods.userFavorGoods.pojo.userFavorGoods;

/**
 * @Description user_favor_goods 服务层
 * @author admin
 * @date 2021-04-19 22:50:00
*/
@Service
public class userFavorGoodsService {

	@Autowired
	private userFavorGoodsDao userFavorGoodsDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<userFavorGoods> findAll() {
		return userFavorGoodsDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<userFavorGoods> findSearch(Map whereMap, int page, int size) {
		Specification<userFavorGoods> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return userFavorGoodsDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<userFavorGoods> findSearch(Map whereMap) {
		Specification<userFavorGoods> specification = createSpecification(whereMap);
		return userFavorGoodsDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public userFavorGoods findById(String id) {
		return userFavorGoodsDao.findById(id).get();
	}

	/**
	* 增加
	* @param userFavorGoods
	*/
	public void add(userFavorGoods userFavorGoods) {
		userFavorGoods.setId( idWorker.nextId()+"" );
		userFavorGoodsDao.save(userFavorGoods);
	}

	/**
	* 修改
	* @param userFavorGoods
	*/
	public void update(userFavorGoods userFavorGoods) {
		userFavorGoodsDao.save(userFavorGoods);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		userFavorGoodsDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<userFavorGoods> createSpecification(Map searchMap) {

		return new Specification<userFavorGoods>() {

			@Override
			public Predicate toPredicate(Root<userFavorGoods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//userid
				if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
					predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
				}
				//goodsid
				if (searchMap.get("goodsid")!=null && !"".equals(searchMap.get("goodsid"))) {
					predicateList.add(cb.like(root.get("goodsid").as(String.class), "%"+(String)searchMap.get("goodsid")+"%"));
				}
				//isdel
				if (searchMap.get("isdel")!=null && !"".equals(searchMap.get("isdel"))) {
					predicateList.add(cb.like(root.get("isdel").as(String.class), "%"+(String)searchMap.get("isdel")+"%"));
				}
				//islike
				if (searchMap.get("islike")!=null && !"".equals(searchMap.get("islike"))) {
					predicateList.add(cb.like(root.get("islike").as(String.class), "%"+(String)searchMap.get("islike")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
